<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->string('slug')->nullable()->after('title')->comment('URL友好名称');
            // 注意：唯一索引会自动创建，不需要单独的 index
            $table->unique('slug'); // 数据库层面确保唯一性
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropUnique(['slug']); // 删除唯一索引
            $table->dropColumn('slug');
        });
    }
};
